package com.yk.anima.servlet;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yk.anima.dao.ProductsHheepMapper;
import com.yk.anima.pojo.ProductsHheep;
import com.yk.anima.response.CommonCode;
import com.yk.anima.response.QueryResponseResult;
import com.yk.anima.response.QueryResult;
import com.yk.anima.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaokai
 * @create 2021-05-15 09:19:05
 * @desc... 对羊的业务管理
 */
@Service
public class ProductsHheepServlet {

    @Autowired
    ProductsHheepMapper productsHheepMapper;

    public List<ProductsHheep> findAll() {
        List<ProductsHheep> productsHheep = productsHheepMapper.findAll();
        if (productsHheep.size() == 0) {
            return null;
        }
        return productsHheep;
    }

    public ProductsHheep findById(int id) {
        ProductsHheep productsHheep = productsHheepMapper.selectByPrimaryKey(id);
        if (productsHheep == null) {
            return null;
        }
        return productsHheep;
    }

    public ResponseResult save(ProductsHheep productsHheep) {
        if (productsHheep == null) {
            productsHheep = new ProductsHheep();
        }
        productsHheep.setSheepId(4);
        productsHheep.setpId(null);
        int number = productsHheepMapper.insertSelective(productsHheep);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }


    public ResponseResult update(int id, ProductsHheep productsHheep) {
        ProductsHheep hheep = productsHheepMapper.selectByPrimaryKey(id);
        if (hheep == null) {
            return new ResponseResult(CommonCode.FINDALLANDNULL);
        }
        hheep.setSheepId(4);
        hheep.setAge(productsHheep.getAge());
        hheep.setGender(productsHheep.getGender());
        hheep.setName(productsHheep.getName());
        hheep.setDescription(productsHheep.getDescription());
        hheep.setPrice(productsHheep.getPrice());
        int number = productsHheepMapper.updateByPrimaryKeySelective(hheep);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }

        return new ResponseResult(CommonCode.FAIL);

    }

    public ResponseResult deleteById(int id) {
        int number = productsHheepMapper.deleteByPrimaryKey(id);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public QueryResponseResult findByPage(int page, int size, String name) {
        if (page<=0){
            page=1;
        }
        if (size<0){
            size=2;
        }
        PageHelper.startPage(page,size);
      Page<ProductsHheep> hheepPage= productsHheepMapper.findByPage(name);
        long total = hheepPage.getTotal();
        List<ProductsHheep> result = hheepPage.getResult();
        QueryResult queryResult = new QueryResult();
        queryResult.setList(result);
        queryResult.setTotal(total);
        return new QueryResponseResult(CommonCode.SUCCESS,queryResult);


    }
}
